<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*" %>
<%
	String profileId = request.getParameter("profileId");
	String serverId = request.getParameter("serverId");
	String jobId = request.getParameter("jobId");
	boolean global=false;
	if(profileId == null && serverId == null && jobId == null){
		global= true;
	}
%>
<h3>Preferences</h3>
<div id="preference-alert-container"></div>
<div class="panel panel-default" id="level-preference-panel" >
	<div class="panel-heading">
		 <i class="fa fa-cog"></i> 
		 <%
		 	if(profileId != null){
		 		out.println("Profiles");
		 	}else if (serverId != null){
		 		out.println("Server");
		 	}else if (jobId != null){
		 		out.println("Schedule Job");
		 	}else{
		 		out.println("Global");
		 	}
		  %>
		  Settings
		  <div class="pull-right">
             <div class="btn-group">
                 <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                     Actions
                     <span class="caret"></span>
                 </button>
                 <ul class="dropdown-menu pull-right" role="menu">
                 	<%
                 		if(!global){
                 	 %>
                     <li><a  id="getFromGlobal" data-toggle="modal" data-target="#globalPreferenceListModel"><i class="fa fa-plus fa-fw"></i>Get Global</a></li>
                    <%
                    	}else{
                    	
                     %>
                     	 <li><a  id="addNewPreference" data-toggle="modal" data-target="#globalPreferenceFormModel" ><i class="fa fa-plus fa-fw"></i>Add</a></li>
                     	 <li><a  id="addDefaultsPreference"><i class="fa fa-plus fa-fw"></i>Add Defaults</a></li>
                     <% } %>
                     <li><a  id="editPreference" ><i class="fa fa-pencil-square-o fa-fw"></i>Edit</a></li>
                     <li><a  id="deletePreference" ><i class="fa fa-times fa-fw"></i>Delete</a></li>
                 </ul>
             </div>
         </div>
	</div>
	<div class="panel-body">
		
             <table class="table table-striped table-bordered table-hover" id="levelPreference-table-list">
  		 	<thead>
           <tr>
            	<th>id</th>
               <th>Preference Name</th>
               <th>Value</th>
              <th>Description</th>
             
           </tr>
     	  </thead>
   		</table>
	</div>
</div>


<div class="modal fade" id="globalPreferenceListModel" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
            <h4 class="modal-title" id="myModalLabel">Global/Default Values</h4>
            </div>
            <div class="modal-body">
                <h3>Global Preferences</h3>
                 <table class="table table-striped table-bordered table-hover" id="globalPreference-table-list">
	     		 	<thead>
			            <tr>
			            	<th>id</th>
			                <th>Preference Name</th>
			                <th>Default Value</th>
			               
			            </tr>
			        </thead>
	       		 </table>
            </div>
            <div class="modal-footer">       
        	</div>
    </div>
  </div>
</div>
<div class="modal fade" id="globalPreferenceFormModel" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
            <h4 class="modal-title">Global Perferences</h4>
            </div>
            <div class="modal-body">
                <h3>Global Preferences</h3>
                <div id="globalPreferenceFormModel-alert-container"></div>
                <form role="form" id="globalPreferenceForm">
                	<div class="form-group">
                        <label>Preference Name:</label>
                         <input id="id"  type="hidden" class="form-control"/>
                         <input id="pName"  class="form-control" />
                    </div>
                    <div class="form-group">
                        <label>Preference Value:</label>
                        <input id="pValue"  class="form-control" />
                        
                    </div>
                    <div class="form-group">
                        <label>Preference Description:</label>
                        <input id="description"  class="form-control" />
                        <input id="scope"  value="GLOBAL" type="hidden" class="form-control"/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
            	
            	<button type="button" id="globalPreferenceFormModelResetBtn"  class="btn btn-default" >Reset</button>
		        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
		        <button type="button" id="globalPreferenceFormModelSvBtn" class="btn btn-primary">Save</button>
	      </div>
    </div>
  </div>
</div>
<script>
	 
	 $(document).ready(function() {
	 	var profileId="<%=profileId %>";
	    var serverId="<%=serverId %>";
	    var global="<%=global%>";
	    var jobId="<%=jobId%>";
	    if(profileId != "null"){
	    	profileId= parseInt(profileId)
	    }
	    if(serverId != "null"){
	    	serverId= parseInt(serverId)
	    }
	    if(jobId != "null"){
	    	jobId= parseInt(jobId)
	    }
	    var prefList=uft.getPreferenceList(profileId, serverId,jobId);
	    var globalList =uft.getPreferencesList("GLOBAL");
	    var selectedGPrefObj= {};
	    var selectedLevelPrefObj={};
	    
	    var globalPreferenceTable= $('#globalPreference-table-list').dataTable( {
	        data: globalList,
	        columns: [
	        	{ data: "id" ,"visible": false,},
	            { data: "pName" } ,
	            { data: "pValue" } 
	        ]
    	});
    	var levelPreferenceTable= $('#levelPreference-table-list').dataTable( {
	        data: prefList,
	        columns: [
	        	{ data: "id" ,"visible": false,},
	            { data: "pName" } ,
	            { data: "pValue" } ,
	            { data: "description" }
	        ]
    	});
	    $('#globalPreference-table-list tbody').on('click','tr', function () { 
	    	selectedGPrefObj= {};
    		$("#globalPreference-table-list tbody tr").removeClass('selected');		
			$(this).addClass('selected');
    		var aPos = globalPreferenceTable.fnGetPosition(this);
   		    var aData = globalPreferenceTable.fnGetData(aPos);
   		    selectedGPrefObj=aData;
   		    $('#globalPreferenceListModel').modal('hide')
   		    
    	 });
    	$('#globalPreferenceListModel').on('hidden.bs.modal', function () {
    		if(!jQuery.isEmptyObject(selectedGPrefObj)){
    			var exist=jQuery.grep(prefList, function(obj) {
				  return obj.pName === selectedGPrefObj.pName; 
				});
				
				if(exist.length === 0){
					 selectedGPrefObj.id="";
					 if(profileId != "null"){
					 	selectedGPrefObj.scope="PROFILE"
					 }
					 if(serverId != "null"){
					 	selectedGPrefObj.scope="SERVER"
					 }
					 if(jobId != "null"){
					 	selectedGPrefObj.scope="JOBS"
					 }
					 uft.saveOrUpdatePreference(profileId, serverId,jobId,selectedGPrefObj, "preference-alert-container")
					 prefList=uft.getPreferenceList(profileId, serverId,jobId);
				}
    			
    		}
    		if(!jQuery.isEmptyObject(prefList) ){
    			levelPreferenceTable.fnClearTable();
				levelPreferenceTable.fnAddData(prefList);
				levelPreferenceTable.fnDraw();
    		}
    		selectedGPrefObj= {};
    		
    	})
	    
	    $('#levelPreference-table-list tbody').on('click','tr', function () { 
	    	selectedLevelPrefObj={}
    		$("#levelPreference-table-list tbody tr").removeClass('selected');		
			$(this).addClass('selected');
    		var aPos = levelPreferenceTable.fnGetPosition(this);
   		    var aData = levelPreferenceTable.fnGetData(aPos);
   		    selectedLevelPrefObj=aData
   		    //console.log(aData);
   		    
    	 });
	    	 	
         $( "#editPreference" ).click(function(){
         	if(!jQuery.isEmptyObject(selectedLevelPrefObj) ){
         		$('#globalPreferenceFormModel').modal('show');
         		dwr.util.setValues(selectedLevelPrefObj);
         	}
         })
         
         $( "#deletePreference" ).click(function(){
         	if(!jQuery.isEmptyObject(selectedLevelPrefObj) ){
         		uft.deletePreference(profileId, serverId,jobId,selectedLevelPrefObj.id, "preference-alert-container")
         		prefList=uft.getPreferenceList(profileId, serverId,jobId)
         		if(jQuery.isEmptyObject(prefList)){
         			levelPreferenceTable.fnClearTable(0);
         			levelPreferenceTable.fnDraw();
         		}else{
         			levelPreferenceTable.fnClearTable();
					levelPreferenceTable.fnAddData(prefList);
			 	    levelPreferenceTable.fnDraw();
         		}
	
         	}
         })
         $('#globalPreferenceFormModel').on('hidden.bs.modal', function () {
         	 prefList=uft.getPreferenceList(profileId, serverId,jobId);      	 
         	 levelPreferenceTable.fnClearTable();
			 levelPreferenceTable.fnAddData(prefList);
			 levelPreferenceTable.fnDraw();
         })
         $( "#globalPreferenceFormModelSvBtn" ).click(function(){
         	var preference={id:null, pName:null, pValue:null, description:null, scope:"GLOBAL"}
         	dwr.util.getValues(preference);
         	uft.saveOrUpdatePreference(profileId, serverId,jobId,preference, "globalPreferenceFormModel-alert-container");
         })
         
         $( "#globalPreferenceFormModelResetBtn" ).click(function(){
         	 $("#globalPreferenceForm")[0].reset();  
         })
        
         $( "#addDefaultsPreference" ).click(function(){
         	uft.addDefaultsPreference();
         })
         
         
         
         
	 })
</script>